﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace PelitaProduksi.View
{
    public partial class Setting : Form
    {
        public string[] keyword = {"server", "dbname", "dbuser", "dbpass", "dbport" };
        public char delimiter = '\t';
        public Setting()
        {
            InitializeComponent();
            initData();
        }

        private void initData()
        {
            string server, dbName, user, pass, port;
            var outputDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
            string[] lines = System.IO.File.ReadAllLines("Setting/Setting.txt");
            foreach(string line in lines)
            {
                string[] temp = line.Split(delimiter);
                if (temp==null || temp.Length != 2) continue;
                switch (temp[0])
                {
                    case "server":
                        server = temp[1];
                        txtServer.Text = server;
                        break;
                    case "dbname":
                        dbName = temp[1];
                        txtDb.Text = dbName;
                        break;
                    case "dbuser":
                        user = temp[1];
                        txtUser.Text = user;
                        break;
                    case "dbpass":
                        pass = temp[1];
                        txtPass.Text = pass;
                        break;
                    case "dbport":
                        port = temp[1];
                        txtPort.Text = port;
                        break;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // Save into the file
            string[] data = new string[keyword.Length];
            data[0] = txtServer.Text;
            data[1] = txtDb.Text;
            data[2] = txtUser.Text;
            data[3] = txtPass.Text;
            data[4] = txtPort.Text;
            // Save into file
            StringBuilder sb = new StringBuilder();
            for (int i=0; i<keyword.Length; i++)
            {
                sb.AppendLine(keyword[i] + delimiter + data[i]);
            }
            System.IO.File.WriteAllText("Setting/Setting.txt", sb.ToString());
            this.Close();
        }
    }
}
